package com.example.elastcisearch;

import lombok.SneakyThrows;
import org.apache.http.HttpHost;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CloseIndexRequest;
import org.elasticsearch.client.indices.CloseIndexResponse;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 索引管理
 */
public class TestIndex {

    private RestHighLevelClient client = null;


    @Before
    public void before() {
        RestClientBuilder builder = RestClient.builder(new HttpHost("192.168.228.104", 9200, "http"));
        client = new RestHighLevelClient(builder);
    }
    
    @Test
    @SneakyThrows
    public void create() {
        CreateIndexRequest request = new CreateIndexRequest("my_idx");
        Settings settings = Settings.builder()
                .put("number_of_shards",1)
                .put("number_of_replicas", 1).build();
        request.settings(settings);

        Map<String, Object> mapping = new HashMap<>();
        Map<String, Object> properties = new HashMap<>();
        
        Map<String, String> name = new HashMap<>();
        name.put("type", "keyword");
        properties.put("name", name);

        Map<String, String> age = new HashMap<>();
        age.put("type", "integer");
        properties.put("age", age);

        Map<String, String> email = new HashMap<>();
        email.put("type", "keyword");
        properties.put("email", email);

        Map<String, String> desc = new HashMap<>();
        desc.put("type", "text");
        properties.put("desc", desc);
        
        mapping.put("properties", properties);
        request.mapping(mapping);

        request.alias(new Alias("idx"));
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(response.isAcknowledged());
        System.out.println(response.isShardsAcknowledged());
        

    }

    @Test
    @SneakyThrows
    public void createByJsonXContent() {
        CreateIndexRequest request = new CreateIndexRequest("user");
        Settings settings = Settings.builder()
                .put("number_of_shards",1)
                .put("number_of_replicas", 1).build();
        request.settings(settings);

        XContentBuilder mappings = JsonXContent.contentBuilder()
                .startObject()
                    .startObject("properties")
                        .startObject("name").field("type", "text").endObject()
                        .startObject("age").field("type", "integer").endObject()
                        .startObject("birthday").field("type", "date").field("format", "yyyy-MM-dd").endObject()
                    .endObject()
                .endObject();
        request.mapping(mappings);

        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(response.isAcknowledged());
        System.out.println(response.isShardsAcknowledged());


    }

    @Test
    @SneakyThrows
    public void createAsync() {
        CreateIndexRequest request = new CreateIndexRequest("idxx");
        Settings settings = Settings.builder()
                .put("number_of_shards",1)
                .put("number_of_replicas", 1).build();
        request.settings(settings);
        
        client.indices().createAsync(request, RequestOptions.DEFAULT, new ActionListener<CreateIndexResponse>() {
            @Override
            public void onResponse(CreateIndexResponse response) {
                System.out.println(response.isAcknowledged());
                System.out.println(response.isShardsAcknowledged());
            }

            @Override
            public void onFailure(Exception e) {
                e.printStackTrace();
            }
        });
    }


    @Test
    @SneakyThrows
    public void delete() {
        DeleteIndexRequest request = new DeleteIndexRequest("my_idx");
        AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
        System.out.println(response.isAcknowledged());
    }

    @Test
    @SneakyThrows
    public void exists() {
        GetIndexRequest request = new GetIndexRequest("my_idx");
        request.local(false);//是否从主节点返回
        request.humanReadable(true);
        request.includeDefaults(false); //是否返回每个索引所有默认配置

        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);

        if (exists) {
            GetIndexResponse getIndexResponse = client.indices().get(request, RequestOptions.DEFAULT);
            System.out.println(getIndexResponse.getDefaultSettings());
            System.out.println(getIndexResponse.getSettings());
            System.out.println(getIndexResponse.getMappings());
            System.out.println(Arrays.toString(getIndexResponse.getIndices()));
            System.out.println(getIndexResponse.getAliases());
        }
        

    }

    @Test
    @SneakyThrows
    public void close() {
        CloseIndexRequest request = new CloseIndexRequest("my_idx");
        CloseIndexResponse response = client.indices().close(request, RequestOptions.DEFAULT);
        System.out.println(response.isAcknowledged());
        


    }
    
}
